﻿/*
# -*- coding: utf-8 -*-
# 注意：

# 若希望将本代码用于商业用途，需先联系作者并获得明确授权。
# 作者不对因使用本代码引发的任何直接或间接损失负责。
# 如有违反上述条款的行为，作者保留追究法律责任的权利。

# 免责声明：
# 请尊重知识产权，遵循相关法律法规。

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# 注意：

# 若希望将本代码用于商业用途，需先联系作者并获得明确授权。
# 作者不对因使用本代码引发的任何直接或间接损失负责。
# 如有违反上述条款的行为，作者保留追究法律责任的权利。

# 免责声明：
# 请尊重知识产权，遵循相关法律法规。

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# 注意：

# 若希望将本代码用于商业用途，需先联系作者并获得明确授权。
# 作者不对因使用本代码引发的任何直接或间接损失负责。
# 如有违反上述条款的行为，作者保留追究法律责任的权利。

# 免责声明：
# 请尊重知识产权，遵循相关法律法规。

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# 注意：

# 若希望将本代码用于商业用途，需先联系作者并获得明确授权。
# 作者不对因使用本代码引发的任何直接或间接损失负责。
# 如有违反上述条款的行为，作者保留追究法律责任的权利。

# 免责声明：
# 请尊重知识产权，遵循相关法律法规。

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# 注意：

# 若希望将本代码用于商业用途，需先联系作者并获得明确授权。
# 作者不对因使用本代码引发的任何直接或间接损失负责。
# 如有违反上述条款的行为，作者保留追究法律责任的权利。

# 免责声明：
# 请尊重知识产权，遵循相关法律法规。

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# 注意：

# 若希望将本代码用于商业用途，需先联系作者并获得明确授权。
# 作者不对因使用本代码引发的任何直接或间接损失负责。
# 如有违反上述条款的行为，作者保留追究法律责任的权利。

# 免责声明：
# 请尊重知识产权，遵循相关法律法规。

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# 注意：

# 若希望将本代码用于商业用途，需先联系作者并获得明确授权。
# 作者不对因使用本代码引发的任何直接或间接损失负责。
# 如有违反上述条款的行为，作者保留追究法律责任的权利。

# 免责声明：
# 请尊重知识产权，遵循相关法律法规。

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.
*/
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using DG.Tweening;


public class CellItem : MonoBehaviour
{
    public int posX, posY;  // position in map
    public int cellType;  // type  // null = -1
    GameController gameController;
    bool isSelected = false;
    [SerializeField]
    Color selectColor;

   // [SerializeField]
   // Sprite normalSprite, selectedSprite;
    [SerializeField]
    Image backgroundImage,itemImage;

    public void Init(GameController _gameController, int _posx, int _posy, int _cellType, Sprite sprite)
    {
        backgroundImage.DOKill();
        this.posX = _posx;
        this.posY = _posy;
        this.cellType = _cellType;
        this.gameController = _gameController;
        itemImage.sprite = sprite;
        isSelected = false;
        backgroundImage.color = Color.white;
        Vector2 size = GetComponent<RectTransform>().sizeDelta;
        itemImage.GetComponent<RectTransform>().sizeDelta = new Vector2(132 * size.x/146f,132 * size.x/146f);
        itemImage.transform.localPosition = new Vector2(-size.x * 7 / 146f,size.x * 7 / 146f );
        
        SetPos();

    }

    void SetPos()
    {
//        Debug.Log(posX +" "+gameController.width);
        Vector2 pos= gameController.GetPos(posX, posY);
        transform.localPosition = pos;
    }


    public void OnPressItem()
    {
        if (!GameMgr.Instance.isPlay)
            return;
        backgroundImage.DOKill();
       
        backgroundImage.color = Color.white;
        isSelected = !isSelected;
        if (isSelected)
        {
            OnSelected();
        }
        else
        {
            OnDeselected();
        }
        gameController.OnPressItem(this);
    }

    public void Hint()
    {
        if (!GameMgr.Instance.isPlay)
            return;

        backgroundImage.DOColor(new Color(1f, 175f/255f, 175/255f), 0.5f).SetLoops(-1, LoopType.Restart);

    }

    private void OnColorUpdated(Color color)
    {
        backgroundImage.color = color;
    }

    public void UpdateSprite(Sprite sprite)
    {
        backgroundImage.color = Color.white;
    //    iTween.Stop(backgroundImage.gameObject);
        itemImage.sprite = sprite;
    }


    /// <summary>
    /// Call when select item
    /// </summary>
    public void OnSelected()
    {
        backgroundImage.color = new Color(198f/255f,206f/255f,49f/255f);
    }

    /// <summary>
    /// Call on deslected
    /// </summary>
    public void OnDeselected()
    {
        backgroundImage.color = Color.white;
        isSelected = false;
    }

    public void MoveItem(Move move)
    {
        switch (move)
        {
            case Move.Up:
                posY++;
                break;
            case Move.Dow:
                posY--;
                break;
            case Move.Right:
                posX++;
                break;
            case Move.Left:
                posX--;
                break;
            default:
                break;
        }

        MoveAnimation();
    }
    void MoveAnimation()
    {
        Vector2 pos= gameController.GetPos(posX, posY);
        transform.DOLocalMove(pos, 0.3f);
    }
}
